﻿using System.Diagnostics;
using System.IO;
using AvalonDock;
using Microsoft.WindowsAPICodePack.Dialogs;
using Microsoft.WindowsAPICodePack.Shell;
using System.Collections.Generic;

namespace UI
{
    /// <summary>
    /// Manages the application window layouts.
    /// </summary>
    public static class LayoutManager
    {
        // TODO change to appdata folder
        /// <summary>
        /// The default lazout folder path.
        /// </summary>
        public static readonly string LayoutPath = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "layouts");

        /// <summary>
        /// Saves the specified layout.
        /// </summary>
        /// <param name="manager">The docking manager.</param>
        public static void Save(DockingManager manager)
        {
            CreateFolder();

            CommonSaveFileDialog dialog = new CommonSaveFileDialog();
            dialog.AddPlace(LayoutPath, FileDialogAddPlaceLocation.Bottom);
            dialog.InitialDirectory = LayoutPath;
            dialog.DefaultExtension = "lyt";
            dialog.Filters.Add(new CommonFileDialogFilter("Layout file", "*.lyt"));
            dialog.Filters.Add(new CommonFileDialogFilter("All files", "*.*"));
            if(dialog.ShowDialog() == CommonFileDialogResult.OK)
            {
                manager.SaveLayout(dialog.FileName);
            }
        }

        /// <summary>
        /// Creates the default folder if not exists.
        /// </summary>
        public static void CreateFolder()
        {
            if(!Directory.Exists(LayoutPath))
            {
                Directory.CreateDirectory(LayoutPath);
            }
        }

        /// <summary>
        /// Gets the layouts from the default folder.
        /// </summary>
        /// <value>The layouts.</value>
        public static IEnumerable<Layout> Layouts
        {
            get
            {
                if (Directory.Exists(LayoutPath))
                {
                    var files = Directory.GetFiles(LayoutPath, "*.lyt");
                    foreach (var file in files)
                    {
                        yield return new Layout(file, Path.GetFileNameWithoutExtension(file));
                    }
                }
            }
        }

        /// <summary>
        /// Restores the specified layout.
        /// </summary>
        /// <param name="manager">The docking manager.</param>
        /// <param name="path">The path to the layout.</param>
        public static void Restore(DockingManager manager, string path)
        {
            manager.RestoreLayout(path);
        }


    }

    /// <summary>
    /// A layout representation.
    /// </summary>
    public class Layout
    {
        public Layout(string path, string displayName)
        {
            Path = path;
            DisplayName = displayName;
        }

        /// <summary>
        /// Gets or sets the path to the layout.
        /// </summary>
        /// <value>The path to the layout.</value>
        public string Path { get; set; }
        /// <summary>
        /// Gets or sets the display name of the layout.
        /// </summary>
        /// <value>The display name.</value>
        public string DisplayName { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return DisplayName;
        }
    }
}
